Saturday, June 28, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

Flutter’s InheritedWidgets: Getting Started | Kodeco

March 27, 2024
in Cloud & Programming
Reading Time: 5 mins read
0 0
A A
0
Share on FacebookShare on Twitter


In Flutter, you’ve probably encountered third-party state management packages like Provider or Bloc. You’ve likely worked with core utilities such as Theme, Navigator, or MediaQuery. These tools all have something in common: they rely on InheritedWidget, a fundamental widget that passes state down the widget tree. In this tutorial, you’ll utilize this power to finish the Weather++ app. By the end, you’ll be able to answer the following questions:

  • What is InheritedWidget and how does it function?
  • How do you utilize InheritedWidget?
Note: This tutorial assumes you have prior knowledge of Dart and the Flutter framework. If you’re new to Flutter, please refer to Getting Started with Flutter.

Getting Started

Download the project by clicking the Download materials button at the top or bottom of this tutorial. Unzip the project, and you’ll find two folders: starter and final. The final directory contains the completed project, while the starter directory is where you’ll begin working. Open the starter project in the latest version of Android Studio or Visual Studio Code, and you’ll see a similar project structure:

The red-outlined folders are specific to this project, while the others are standard Flutter boilerplate.

  • assets/secrets.json: JSON file for storing keys like the API key; this key will be used to fetch weather data in later steps.
  • lib/location: Contains Dart files for managing user location. More files will be added to this directory later.
  • lib/weather: Handles weather-specific Dart files such as widgets and data classes.
  • lib/constants.dart: Holds app-wide constants.
  • lib/home.dart: Includes widgets seen when the app is launched for the first time.
  • lib/main.dart: Initializes the app and wraps the HomeWidget in a MaterialApp.
  • lib/secrets.dart: Contains logic for loading the secrets in assets/secrets.json.

Now, open pubspec.yaml and click the Pub get tab in your IDE. Run the project to view it on your target emulator or device:

Empty starter project

This represents the basic structure of the app. The white boxes serve as placeholders for widgets that will be replaced as you progress through this tutorial.

Overview of Inherited Widgets

Begin this section by exploring the concept of InheritedWidget, understanding its definition, importance, and functionality. Later, delve into how it differs from StatefulWidget and StatelessWidget.

What Is an InheritedWidget?

InheritedWidget is a core widget in the Flutter framework that facilitates the efficient passing of state down the widget tree. It utilizes the build context to share state and updates dependent widgets when this state changes. Without InheritedWidget, sharing ThemeData like light and dark mode between a parent and child widget would require manual passing of the theme to each widget. This not only creates tight coupling but also fails to automatically rebuild widgets when the theme changes, potentially causing UI inconsistencies. By leveraging InheritedWidget, child widgets can access the latest ThemeData as shown below:

class ChildWidget extends StatelessWidget {
  const ChildWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return IconButton(
        onPressed: () {
          // TODO: Implement functionality to change the app theme.
        },
        icon: Icon(Theme.of(context).brightness == Brightness.dark
            ? Icons.nightlight
            : Icons.sunny));
  }
}

With this approach, you no longer need to pass ThemeData to the widget, and any theme changes will trigger automatic rebuilding.

Differences Between StatelessWidget, StatefulWidget, and InheritedWidget

A StatelessWidget is immutable, meaning its properties cannot be changed once set. It is suitable for static content that does not require modification by the widget itself. In contrast, a StatefulWidget can be mutable, maintaining separate state object(s) that can change over the widget’s lifecycle. This makes it ideal for dynamic content that may evolve over time. On the other hand, InheritedWidget is a special widget designed to efficiently propagate information down the widget tree. Instead of manually passing data to each widget, descendants of an InheritedWidget can directly access the data it holds, making it a robust tool for state propagation and management.

State Propagation and State Management

To demonstrate and comprehend InheritedWidget, you’ll focus on the Weather++ app, the project downloaded in the previous steps. Using InheritedWidget, you’ll construct a location picker whose selection state can be read and modified from any widget in the build tree. This selected location will be used to retrieve and display current and future weather forecasts.

State Propagation with InheritedLocation

The initial step in utilizing an InheritedWidget is to subclass it. Therefore, in the starter project, create the file inherited_location.dart within the location package and add the following code:

import 'package:flutter/widgets.dart';
import 'location_data.dart';

class InheritedLocation extends InheritedWidget {
  final LocationData? location;

  const InheritedLocation(
      {Key? key, required Widget child, required this.location})
      : super(key: key, child: child);
}

InheritedLocation is an InheritedWidget that triggers a rebuild of all dependent widgets when the location changes. LocationData serves as a data container holding the latitude, longitude, and name of a specific location. The latitude and longitude are used to fetch weather data from OpenWeather, a popular service for real-time weather information. The name will be displayed in the location picker widget.

But how does InheritedLocation know when to rebuild? This is where updateShouldNotify() comes into play. Override it below the constructor as shown below:

@override
bool updateShouldNotify(InheritedLocation oldWidget) {
  return oldWidget.location != location ||
      oldWidget.location?.name.isEmpty == true;
}

This method triggers rebuilding of dependent widgets when the location changes or if its name is empty. Further details on the empty condition will be discussed later.

How does InheritedLocation identify its dependencies? The answer lies in context.dependOnInheritedWidgetOfExactType(). Calling this using the context of the dependent widget accomplishes two things: registering the calling widget as a dependent of InheritedLocation and returning a reference to the InheritedLocation instance.

For brevity, encapsulate this code within a helper function called of() in the InheritedLocation class, below updateShouldNotify():

static InheritedLocation of(BuildContext context) {
  final result =
      context.dependOnInheritedWidgetOfExactType();
  assert(result != null, 'No InheritedLocation found in context');
  return result!;
}

This method allows you to utilize InheritedLocation.of(context) similar to Theme, Provider, and other widgets powered by InheritedWidget.



Source link

Tags: FlutterâsInheritedWidgetsKodecoStarted
Previous Post

What is cognitive overload in UX and how can you prevent it?

Next Post

New software enables blind and low-vision users to create interactive, accessible charts

Related Posts

Top 20 Javascript Libraries You Should Know in 2024
Cloud & Programming

Top 20 Javascript Libraries You Should Know in 2024

June 10, 2024
Simplify risk and compliance assessments with the new common control library in AWS Audit Manager
Cloud & Programming

Simplify risk and compliance assessments with the new common control library in AWS Audit Manager

June 6, 2024
Simplify Regular Expressions with RegExpBuilderJS
Cloud & Programming

Simplify Regular Expressions with RegExpBuilderJS

June 6, 2024
How to learn data visualization to accelerate your career
Cloud & Programming

How to learn data visualization to accelerate your career

June 6, 2024
BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager
Cloud & Programming

BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager

June 6, 2024
Copilot Studio turns to AI-powered workflows
Cloud & Programming

Copilot Studio turns to AI-powered workflows

June 6, 2024
Next Post
New software enables blind and low-vision users to create interactive, accessible charts

New software enables blind and low-vision users to create interactive, accessible charts

Intel Gaudi 2 Remains Only Benchmarked Alternative to NV H100 for GenAI Performance

Intel Gaudi 2 Remains Only Benchmarked Alternative to NV H100 for GenAI Performance

Zircuit Launches Build to Earn Program to Reward Ecosystem Contributors – Blockchain News, Opinion, TV and Jobs

Zircuit Launches Build to Earn Program to Reward Ecosystem Contributors – Blockchain News, Opinion, TV and Jobs

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
23 Plagiarism Facts and Statistics to Analyze Latest Trends

23 Plagiarism Facts and Statistics to Analyze Latest Trends

June 4, 2024
How ‘Chain of Thought’ Makes Transformers Smarter

How ‘Chain of Thought’ Makes Transformers Smarter

May 13, 2024
Amazon’s Bedrock and Titan Generative AI Services Enter General Availability

Amazon’s Bedrock and Titan Generative AI Services Enter General Availability

October 2, 2023
Is C.AI Down? Here Is What To Do Now

Is C.AI Down? Here Is What To Do Now

January 10, 2024
The Importance of Choosing a Reliable Affiliate Network and Why Olavivo is Your Ideal Partner

The Importance of Choosing a Reliable Affiliate Network and Why Olavivo is Your Ideal Partner

October 30, 2023
Managing PDFs in Node.js with pdf-lib

Managing PDFs in Node.js with pdf-lib

November 16, 2023
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In